package com.yanqu.road.server.manager.activity.musicroomrank;

import com.yanqu.road.dao.impl.activity.musicroomrank.CrossMusicRoomRankRoundDaoImpl;
import com.yanqu.road.entity.activity.musicroomrank.CrossMusicRoomRankRoundData;
import com.yanqu.road.entity.activity.musicroomrank.CrossMusicRoomRankShowData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.MusicRoomRankProto;
import com.yanqu.road.server.manager.user.CrossUserMgr;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class MusicRoomRankHistoryShowRoom {
    private int activityId;
    private CrossMusicRoomRankRoundData roundData;
    private MusicRoomRankProto.MusicRoomRankHistoryShowTemp roundTemp = null;
    private List<MusicRoomRankProto.MusicRoomRankShowTemp> showTempList = new ArrayList<>();
    /**
     * 参赛的玩家美女
     */
    private Map<Long, Map<Integer, Integer>> joinBeautyMap = new HashMap<>();
    /**
     * 玩家在本场获得的总分
     */
    private Map<Long, Long> userRankScoreMap = new HashMap<>();

    /**
     * 等待结算的游戏服
     */
    private Map<Long, MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.Builder> waitSettleBackServerMap = new ConcurrentHashMap<>();

    public MusicRoomRankHistoryShowRoom(CrossMusicRoomRankRoundData roundData, List<CrossMusicRoomRankShowData> showList) {
        this.activityId = roundData.getActivityId();
        this.roundData = roundData;

        showList.sort((o1, o2) -> {
            return Integer.compare(o1.getRank(), o2.getRank());
        });

        MusicRoomRankProto.MusicRoomRankShowTemp.Builder firstShowMsg = null;
        for (CrossMusicRoomRankShowData showData : showList) {
            MusicRoomRankProto.MusicRoomRankShowTemp.Builder builder = MusicRoomRankProto.MusicRoomRankShowTemp.newBuilder();
            builder.setUserId(showData.getUserId());
            builder.setBeautyId(showData.getBeautyId());
            builder.setSkinId(showData.getSkinId());
            builder.setScore(showData.getShowScore());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(showData.getUserId());
            if (userBaseInfo == null) {
                userBaseInfo = new UserBaseInfo();
            }
            builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            builder.setIndex(showData.getJoinPos());
            builder.setRank(showData.getRank());
            builder.setRankScore(showData.getRankScore());
            builder.setBuffAddScore(showData.getBuffAddScore());
            builder.setCostType(showData.getCostType());
            showTempList.add(builder.build());

            if (showData.getRank() == 1) {
                firstShowMsg = builder;
            }

            Map<Integer, Integer> skinMap = joinBeautyMap.get(showData.getUserId());
            if (skinMap == null) {
                skinMap = new HashMap<>();
                joinBeautyMap.put(showData.getUserId(), skinMap);
            }
            skinMap.put(showData.getBeautyId(), showData.getSkinId());

            Long oldRankScore = userRankScoreMap.getOrDefault(showData.getUserId(), 0L);
            userRankScoreMap.put(showData.getUserId(), oldRankScore + showData.getRankScore());
        }

        MusicRoomRankProto.MusicRoomRankHistoryShowTemp.Builder builder = MusicRoomRankProto.MusicRoomRankHistoryShowTemp.newBuilder();
        builder.setTheme(roundData.getTheme());
        builder.setRound(roundData.getRound());
        builder.setStartTime(roundData.getStartTime());
        builder.setEndTime(roundData.getFinishTime());
        if (firstShowMsg != null) {
            builder.setFirstShow(firstShowMsg);
        }
        roundTemp = builder.build();

        if (!roundData.getWaitSettleServerSet().isEmpty()) {//入库后还有没同步完的评选结果
            MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.Builder syncBuilder = parseSyncBuilder();
            for (Long serverId : roundData.getWaitSettleServerSet()) {
                waitSettleBackServerMap.put(serverId, syncBuilder);
            }
        }
    }

    public int getRound() {
        return roundData.getRound();
    }

    public MusicRoomRankProto.MusicRoomRankHistoryShowTemp getRoundTemp() {
        return roundTemp;
    }

    public List<MusicRoomRankProto.MusicRoomRankShowTemp> getShowTempList() {
        return showTempList;
    }

    public boolean isUserJoined(long userId) {
        return joinBeautyMap.containsKey(userId);
    }

    public MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.Builder parseSyncBuilder() {
        MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.Builder syncBuilder = MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.newBuilder();
        syncBuilder.setActivityId(activityId);
        syncBuilder.setRound(roundTemp.getRound());
        syncBuilder.setTheme(roundTemp.getTheme());
        syncBuilder.addAllRankData(showTempList);
        return syncBuilder;
    }

    public void addSyncResultMsgMap() {
        Map<Long, MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.Builder> serverMsgMap = new ConcurrentHashMap<>();

        MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.Builder syncBuilder = parseSyncBuilder();

        for (MusicRoomRankProto.MusicRoomRankShowTemp showTemp : showTempList) {
            long serverId = showTemp.getUserBaseInfo().getServerId();
            if (!serverMsgMap.containsKey(serverId)) {
                serverMsgMap.put(serverId, syncBuilder);
            }
        }

        waitSettleBackServerMap = serverMsgMap;
    }

    public Map<Long, MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.Builder> getSyncResultMsgMap() {
        return waitSettleBackServerMap;
    }

    public void removeSyncResultMsgMap(long serverId) {
        waitSettleBackServerMap.remove(serverId);
        roundData.getWaitSettleServerSet().remove(serverId);
        roundData.setUpdateOption();
    }

    public void save() {
        if (roundData.isInsertOption()) {
            new CrossMusicRoomRankRoundDaoImpl().add(roundData);
        } else if (roundData.isUpdateOption()) {
            new CrossMusicRoomRankRoundDaoImpl().update(roundData);
        }
    }

    public boolean isAllSyncBack() {
        return waitSettleBackServerMap.isEmpty();
    }

    public Map<Long, Long> getUserRankScoreMap() {
        return userRankScoreMap;
    }

    public long getUserRankScore(long userId) {
        return userRankScoreMap.getOrDefault(userId, 0L);
    }
}
